package com.xiaoyu.client.ui.activity.help;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.xiaoyu.client.R;
import com.xiaoyu.client.adapter.seek.SeekTypeAdapter;
import com.xiaoyu.client.model.seek.SeekTypeBean;
import com.xiaoyu.client.model.seek.SeekTypeBean.SeekTypeParam;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.ui.widget.NestedGridView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 求助类别选择页面
 */
public class SeekTypeActivity extends BaseActivity implements View.OnClickListener {
    public static final String SEEK_TYPE_ID = "seek_type_id";
    public static final String SEEK_TYPE_NAME = "seek_type_name";
    @BindView(R.id.activity_seek_type_title_back_btn)
    ImageView backBtn;//返回按钮
    @BindView(R.id.activity_seek_type_list_layout)
    NestedGridView mTypeLayout;//求助类别列表容器

    private SeekTypeAdapter mAdapter;
    private List<SeekTypeParam> mlist = new ArrayList<>();
    private String typeId,typeName;//选择的求助类别id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_type);
        ButterKnife.bind(this);
        parseIntent();
        initView();
    }

    private void parseIntent() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            typeId = bundle.getString(SEEK_TYPE_ID);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getSeekType();
    }

    private void initView() {
        mAdapter = new SeekTypeAdapter(this, mlist);
        mTypeLayout.setAdapter(mAdapter);
        mAdapter.setOnItemTypeClickListener(typeClickListener);
        backBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v == backBtn) {
            onBackPressed();
        }
    }

    /**
     * 类别item点击监听
     */
    private SeekTypeAdapter.OnItemTypeClickListener typeClickListener = new SeekTypeAdapter.OnItemTypeClickListener() {
        @Override
        public void onTypeClick(CheckBox checkBox, boolean isChecked, int position) {
            String id = mlist.get(position).getSeephelpid();
            String name = mlist.get(position).getSeephelpname();
            if (id.equals(typeId)) {
                typeId = "";
                name = "请选择求助类别";
            } else {
                typeId = id;
                typeName = name;
            }
            Intent intent = new Intent();
            intent.putExtra(SEEK_TYPE_ID, typeId);
            intent.putExtra(SEEK_TYPE_NAME, typeName);
            setResult(0, intent);
            finish();
        }
    };


    /**
     * 获取求助类别信息
     */
    private void getSeekType() {
        NetworkManager.getSeekType(new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                parseSeekTypeSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    private void parseSeekTypeSuc(String result) {
        SeekTypeBean bean = new Gson().fromJson(result, SeekTypeBean.class);
        mlist.clear();
        mlist.addAll(bean.getType());
        mAdapter.setSelectId(typeId);
    }
}
